package com.xgg.taiwuinfo.fragement;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.xgg.taiwuinfo.MainActivity;
import com.xgg.taiwuinfo.R;
import com.xgg.taiwuinfo.adapter.FilterAdapter;
import com.xgg.taiwuinfo.filter.Filter;
import com.xgg.taiwuinfo.filter.FilterList;
import com.xgg.taiwuinfo.gongfa.base.Gongfa;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FilterListFragement extends Fragment implements AdapterView.OnItemClickListener {
    public List<Map<String, String>> mFilterDataList;
    ListView list_filter;
    FilterAdapter mAdapter;
    Filter mCurFilter;
    ArrayList<Gongfa> mLocalList = new ArrayList<>();         //过滤及排序后的功法列表

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragement_filterlist, container, false);
    }

    @Override
    public void onStart()
    {
        super.onStart();
        list_filter = getView().findViewById(R.id.list_filter);
        list_filter.setOnItemClickListener(this);
        ReloadFilter();
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
    {
        if (mFilterDataList.size() < position + 1)
        {
            //下标越界
            return;
        }
        Map<String, String> map = mFilterDataList.get(position);
        String id = map.get("id");//这是总表中的序号
        MainActivity act = (MainActivity)getActivity();
        act.NavToFilterDetail(id);
    }

    public void ReloadFilter()
    {
        mFilterDataList = GetFilterData();
        mAdapter = new FilterAdapter(getActivity(), mFilterDataList, 0, null, null);
        list_filter.setAdapter(mAdapter);

    }

    List<Map<String, String>> GetFilterData()
    {
        ArrayList<Map<String, String>> list = new ArrayList<> ();
        for (int i = 0; i < FilterList.getList().size(); i++)
        {
            Filter ft = FilterList.getList().get(i);
            Map<String, String> map = new HashMap<>();
            map.put("id" , i + "");
            map.put("name", ft.mName);
            list.add(map);
        }
        return list;
    }
}
